app.controller('commodityContorller', ['$scope', 'ajax', '$state','commodityService','Flash', 'uploadService','SERVER_URL',function ($scope, ajax, $state,commodityService,Flash,uploadService,SERVER_URL) {
    $scope.customer = {

    };
    $scope.data = {
        isShowAddView : false,
        isShowUpdate: false,
        popViewTitle:"添加商品",
        isShowbtn:false,
        isAdd : true
    };

    $scope.s = {
        name : ""
    };
    $scope.f = {
        name : ""
    }

    $scope.queryData = {
        area : "",
        categoryItem : ""
    };


    //添加商品分类
    ajax.get({
        url: 'back/commodityCategory/list',
        data:{
            level:1
        },
        debug: false
    }).then(function (data) {
        $scope.f_class=data;
        $scope.$watch('f.name', function(data) {
            if($scope.f){
                ajax.get({
                    url: 'back/commodityCategory/list',
                    data:{
                        parentId:$scope.f.name.categoryId
                    },
                    debug: false
                }).then(function (data) {
                    $scope.s_class=data;
                    $scope.s={
                        obj:data
                    };
                    $scope.$watch('s.name', function(data1) {
                    });
                })
            }
        });
    })

    $scope.showAddView = function(){
        $scope.f_add = {
            name : ""
        };
        $scope.s_add = {
            name : ""
        }
        $scope.data.popViewTitle = "添加商品";
        $scope.data.isAdd = "true";
        $scope.data.isShowAddView = true;
        $scope.customer=null;
    };
    $scope.doQuery = function(){
        list(1);
    }

    // 商品列表
    function list(page) {
        var categoryId = "";
        if($scope.queryData.categoryItem){
            categoryId = $scope.queryData.categoryItem.categoryId;
        }
        commodityService.list({
            currentPage: page,
            pageSize: 10,
            categoryId : categoryId,
            area : $scope.queryData.area,
            name_like : $scope.queryData.name_like
        }).then(function (result) {
            console.log(result);
            $scope.page = result;
        });
    }

    list(1);

    //分页
    $scope.pageChanged = function (e) {
        list(e);
    };

    //删除商品
    $scope.commodityDel = function (data) {
        var returnKey = confirm("确定删除该商品吗");
        if (returnKey) {
            ajax.post({
                url: 'back/commodity/delete',
                data: {
                    commodityIds: data
                }
            }).then(function (result) {


                Flash.create('success', '删除成功', 'custom-class');
                list($scope.page.currentPage);


            })
        }
    }
    //新增商品初始化查询

    //重置
    $scope.reset=function(){
        $scope.f = {
            name : ""
        };
        $scope.s = {
            name : ""
        };
        $scope.queryData = {
            area : "",
            categoryItem : ""
        };
        $scope.queryData.name_like='';
    }

    //添加商品分类

    ajax.get({
        url: 'back/commodityCategory/list',
        data:{
            level:1
        },
        debug: false
    }).then(function (data) {


        $scope.f_add_class=data;

        $scope.$watch('f_add.name', function(data) {
            if($scope.f_add){
                ajax.get({
                    url: 'back/commodityCategory/list',
                    data:{
                        parentId:$scope.f_add.name.categoryId
                    },
                    debug: false
                }).then(function (data) {
                    $scope.s_add_class=data;

                    //加载默认值
                    if($scope.data.defaultCategoryId){

                        for(var i=0;i<$scope.s_add_class.length;i++){

                            if($scope.s_add_class[i].categoryId == $scope.data.defaultCategoryId){
                                $scope.s_add.name = $scope.s_add_class[i];
                            }

                        }

                    }
                })
            }
        });
    })
    //提交商品
    $scope.commodityDetail = function(data){

        data.categoryId=$scope.s_add.name.categoryId;
        data.imageId = $scope.imageId;
        data.imageUrl  = $scope.imageUrl;
        console.log(data);
        //判断是添加还是修改
        if($scope.data.isAdd){
            ajax.post({
                url: 'back/commodity/add',
                data: data,
                debug: true
            }).then(function(){
                Flash.create('success', "保存成功", 'custom-class');
                $scope.data.isShowAddView =false;
                list($scope.page.currentPage);
                $state.go('admin.commodity',null,{
                    reload:true
                });
            })
        }else{
            ajax.post({
                url: 'back/commodity/update',
                data: data,
                debug: true
            }).then(function(){
                Flash.create('success', "修改成功", 'custom-class');
                $scope.data.isShowAddView =false;
                list($scope.page.currentPage);
                $state.go('admin.commodity',null,{
                    reload:true
                });
            })

        }

    }

    //初始化商品类目
    $scope.f_add = {
        name : ""
    };
    $scope.s_add = {
        name : ""
    }


    //点击修改商品
    $scope.updateCommodity = function(commodityId){
        $scope.data.isShowAddView = true;
        $scope.data.popViewTitle = "修改商品";
        $scope.data.isAdd = false;
        $scope.pic = '';
        $scope.imageUrl = '';
        $scope.imageId = '';

        ajax.get({
            url: 'back/commodity/query',
            data: {
                commodityId: commodityId
            },
            debug: false
        }).then(function (data) {
            console.log(data);
            $scope.f_add.name=data.categoryName;
            $scope.s_add.name=data.categoryParentName;
            $scope.customer = data;
            //初始化类目选择
            for(var i=0;i<$scope.f_add_class.length;i++){
                if($scope.f_add_class[i].categoryId == data.categoryParentId){
                    $scope.f_add.name = $scope.f_add_class[i];
                    $scope.data.defaultCategoryId = data.categoryId;
                }
            }

        })

    };

    //上传图片
    $scope.uploadPic = function(data){
        uploadService.upload(data).then(function(result){
            $scope.imageUrl = result.url;
            $scope.imageId = result.attachmentId;
        });
    }

    //导出表格
    $scope.exportAllGoods = function(){
        //ajax.get({
        //    url: 'back/commodity/exportExcel'
        //}).then(function () {
        //})
        location.href=SERVER_URL+'back/commodity/exportExcel';
    }



}]);